package org.example.ChatroomTest;

import org.junit.jupiter.api.*;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.ValueSource;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import java.util.concurrent.TimeUnit;

import static java.lang.Thread.sleep;
import static java.util.concurrent.TimeUnit.SECONDS;
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class Tests extends IniBrowser{


    @Order(1)
@ParameterizedTest
@CsvSource({"'http://120.26.95.48:8888/login.html','zhangsan','123','http://120.26.95.48:8888/client.html'"})
    void login(String curUrl , String username,String password,String expectedUrl) throws InterruptedException {
        webDriver.get(curUrl);
        webDriver.manage().timeouts().implicitlyWait(1, SECONDS);
        webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
        webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
        webDriver.findElement(By.cssSelector("#submit")).click();
        sleep(1000);
        webDriver.switchTo().alert().accept();
        sleep(1000);
        Assertions.assertEquals(webDriver.getCurrentUrl(),expectedUrl);
        System.out.println("登录测试通过");
    }
    @Order(2)
    @ParameterizedTest
    @ValueSource(strings = {"zhangsan"})
    void userInformation(String username) throws InterruptedException {
      WebElement ret = webDriver.findElement(By.cssSelector("body > div.client-container > div > div.left > div.user"));
      sleep(1000);
        System.out.println("登录用户是："+ret.getText());
       Assertions.assertEquals(ret.getText(),username);
        System.out.println("用户信息测试通过");
    }
@Order(3)
@Test
    void selectSession() throws InterruptedException {
        webDriver.findElement(By.xpath("//*[@id=\"session-list\"]/li[1]")).click();
sleep(1000);
        String r1 = webDriver.findElement(By.cssSelector("#session-list > li.selected > h3")).getText();
        sleep(1000);
        String r2 = webDriver.findElement(By.cssSelector("body > div.client-container > div > div.right > div.title")).getText();
        sleep(1000);
        Assertions.assertEquals(r1,r2);
        System.out.println("点击试通过");

    }
@Order(4)
@Test
    void selectFriend() throws InterruptedException {
        webDriver.findElement(By.cssSelector("body > div.client-container > div > div.left > div.tab > div.tab-friend")).click();
        sleep(1000);
        String r1 =  webDriver.findElement(By.cssSelector("#friend-list > li:nth-child(1) > h4")).getText();
        webDriver.findElement(By.cssSelector("#friend-list > li:nth-child(1) > h4")).click();
        sleep(1000);
        String r2 = webDriver.findElement(By.cssSelector("#session-list > li.selected > h3")).getText();
        sleep(1000);
        Assertions.assertEquals(r1,r2);
        System.out.println("点击好友测试通过");

    }
@Order(5)
@ParameterizedTest
@ValueSource(strings = {"你好啊" })

    void sendMeaasge(String message) throws InterruptedException {
        webDriver.findElement(By.cssSelector("body > div.client-container > div > div.right > textarea")).sendKeys(message);
        sleep(1000);

        webDriver.findElement(By.cssSelector("body > div.client-container > div > div.right > div.ctrl > button")).click();
        sleep(2000);
        String s = webDriver.findElement(By.cssSelector("#session-list > li.selected > p")).getText();

        Assertions.assertEquals(s,message);
    System.out.println("发送消息测试通过");

    }
@Order(6)
    @ParameterizedTest
    @CsvSource({"'huang','123','http://120.26.95.48:8888/register.html','http://120.26.95.48:8888/login.html'"})

    void register(String username,String password,String curUrl,String expectedUrl) throws InterruptedException {
        webDriver.get(curUrl);
        sleep(1000);
        webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
        webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
        sleep(1000);
        webDriver.findElement(By.cssSelector("#submit")).click();
        sleep(1000);
    webDriver.switchTo().alert().accept();
    Assertions.assertEquals(webDriver.getCurrentUrl(),expectedUrl);
    System.out.println("注册测试通过");
    }


}
